Click here to Skip to main content
16,017,069 members
Home / Discussions / C#
   

C#

 
GeneralRe: deployment in window Pin
Sonia Gupta6-Aug-07 23:27
Sonia Gupta6-Aug-07 23:27 
GeneralRe: deployment in window Pin
satsumatable6-Aug-07 23:33
satsumatable6-Aug-07 23:33 
GeneralRe: deployment in window Pin
Sonia Gupta6-Aug-07 23:36
Sonia Gupta6-Aug-07 23:36 
GeneralRe: deployment in window Pin
satsumatable6-Aug-07 23:48
satsumatable6-Aug-07 23:48 
GeneralRe: deployment in window Pin
Sonia Gupta6-Aug-07 23:53
Sonia Gupta6-Aug-07 23:53 
GeneralRe: deployment in window Pin
satsumatable6-Aug-07 23:58
satsumatable6-Aug-07 23:58 
Questioncan i use optional parameters Pin
Sonia Gupta6-Aug-07 19:38
Sonia Gupta6-Aug-07 19:38 
AnswerRe: can i use optional parameters Pin
Hessam Jalali6-Aug-07 20:19
Hessam Jalali6-Aug-07 20:19 
Here is from MSDN

Some languages (such as the Managed Extensions for C++ and Microsoft Visual Basic 2005) support the assignment of default values to arguments. For example, the following is a legitimate Visual Basic 2005 declaration that has default values for two of the arguments.

<code>
Visual Basic  Copy Code 
Public Sub MyMethod (a as Integer, _
                     Optional b as Double = 1.2, _
                     Optional c as Integer = 1)
 
</code>
You can use a parameter attribute to assign a default parameter value. 

In Visual Basic and C++, optional parameters can be omitted when the method is called. In C# values must be specified for optional arguments.

For example, all the following Visual Basic and C++ examples are valid calls for MyMethod.

Visual Basic  Copy Code 
<code>
MyMethod(10, 55.3, 12)
MyMethod(10, 1.3) ' c == 1
MyMethod(11) ' b == 1.2, c == 1
 </code>

C++  Copy Code 
<code>
MyMethod(10, 55.3, 12);
MyMethod(10, 1.3);   // c == 1
MyMethod(11);        // b == 1.2, c == 1
</code>

To retrieve the default value of an argument using reflection, get a ParameterInfo object for the parameter, and then retrieve the default value using the ParameterInfo.DefaultValue property. If there is no default value, the property returns Value.DBNull.

The following example displays the default values for MyMethod to the console.

Visual Basic  Copy Code
<code> 
Dim m As MethodInfo = t.GetMethod("MyMethod")
Dim ps As ParameterInfo() = m.GetParameters()
Dim i As Integer
For i = 0 To ps.Length - 1
    Console.WriteLine("Default Value == {0}", ps(i).DefaultValue)
Next i
 </code>
C#  Copy Code
<code> 
MethodInfo m = t.GetMethod("MyMethod");
ParameterInfo[] ps = m.GetParameters();
for (int i = 0; i < ps.Length; i++) 
{
    Console.WriteLine("Default Value == {0}", ps[i].DefaultValue);
}
 </code>
C++  Copy Code
<code> 
MethodInfo m = t->GetMethod("MyMethod");
ParameterInfo[] ps = m->GetParameters();
for (int i = 0; i < ps.Length; i++) 
{
    Console::WriteLine(S"Default Value == {0}", ps[i]->DefaultValue);
}
 
</code>
To invoke methods that have arguments with default values, use Type.Missing as a parameter value to the InvokeMember method. This enables the late-binding service to use the default value for the indicated parameter value. If Type.Missing is passed for a parameter that has no default value, an ArgumentException is thrown. It is important to note that not all compilers' binding mechanisms might respect these rules for Type.Missing. Some binders might not support this functionality, or might treat Type.Missing differently. When using Type.Missing, the default values do not have to be trailing.

The C# language does not support default arguments.

The following Visual Basic 2005 example shows how to use Reflection to invoke methods that have default arguments.

  Copy Code 
<code>
Option Strict Off
Imports System
Imports System.Reflection
Public Class OptionalArg
    Public Sub MyMethod (a As Integer, Optional b As Double = 1.2, Optional c As Integer=1)
        Console.WriteLine("a = " & a & " b = " & b & " c = " & c)
    End Sub
End Class
Module Module1
    Sub Main()
        Dim o As New OptionalArg
        Dim t As Type
        t = GetType(OptionalArg)
        Dim Param As Object()= {10, 20, 30}
        t.InvokeMember("MyMethod", _
                        BindingFlags.Public Or _
                        BindingFlags.Instance Or _
                        BindingFlags.InvokeMethod Or _
                        BindingFlags.OptionalParamBinding, _
                        Nothing, _
                        o, _
                        New Object() {10, 55.3, 12})
        t.InvokeMember("MyMethod", _
                        BindingFlags.Public Or _
                        BindingFlags.Instance Or _
                        BindingFlags.InvokeMethod Or _
                        BindingFlags.OptionalParamBinding, _
                        Nothing, _
                        o, _
                        New Object() {10, 1.3, Type.Missing})
        t.InvokeMember("MyMethod", _
                        BindingFlags.Public Or _
                        BindingFlags.Instance Or _
                        BindingFlags.InvokeMethod Or _
                        BindingFlags.OptionalParamBinding, _
                        Nothing, _
                        o, _
                        New Object() {10, Type.Missing, Type.Missing})
    End Sub
End Module
 
</code>
When using the preceding technique, trailing default arguments are considered even when the caller specifies no value. This is the most common way to invoke methods with default arguments.

AnswerRe: can i use optional parameters [modified] Pin
Tormod Fjeldskaar6-Aug-07 20:25
Tormod Fjeldskaar6-Aug-07 20:25 
GeneralRe: can i use optional parameters Pin
Sonia Gupta6-Aug-07 20:55
Sonia Gupta6-Aug-07 20:55 
GeneralRe: can i use optional parameters Pin
Christian Graus6-Aug-07 21:01
protectorChristian Graus6-Aug-07 21:01 
GeneralRe: can i use optional parameters Pin
Sonia Gupta6-Aug-07 21:10
Sonia Gupta6-Aug-07 21:10 
AnswerRe: can i use optional parameters Pin
Diana Fernandez6-Aug-07 21:12
Diana Fernandez6-Aug-07 21:12 
AnswerRe: can i use optional parameters Pin
Pete O'Hanlon6-Aug-07 21:47
mvePete O'Hanlon6-Aug-07 21:47 
QuestionString to stream [modified] Pin
mpavas6-Aug-07 19:35
mpavas6-Aug-07 19:35 
AnswerRe: String to stream Pin
Hessam Jalali6-Aug-07 20:08
Hessam Jalali6-Aug-07 20:08 
Questiongoto in c# Pin
Sonia Gupta6-Aug-07 19:18
Sonia Gupta6-Aug-07 19:18 
AnswerRe: goto in c# Pin
Christian Graus6-Aug-07 19:22
protectorChristian Graus6-Aug-07 19:22 
GeneralRe: goto in c# Pin
Sonia Gupta6-Aug-07 19:25
Sonia Gupta6-Aug-07 19:25 
QuestionRe: goto in c# Pin
Urs Enzler6-Aug-07 23:18
Urs Enzler6-Aug-07 23:18 
QuestionSending parameters to method by reference ... ? Pin
Yanshof6-Aug-07 19:18
Yanshof6-Aug-07 19:18 
AnswerRe: Sending parameters to method by reference ... ? Pin
Hessam Jalali6-Aug-07 20:26
Hessam Jalali6-Aug-07 20:26 
AnswerRe: Sending parameters to method by reference ... ? Pin
Luc Pattyn7-Aug-07 2:12
sitebuilderLuc Pattyn7-Aug-07 2:12 
QuestionMedia Pin
sulabh20206-Aug-07 18:52
sulabh20206-Aug-07 18:52 
AnswerRe: Media Pin
Ravi Bhavnani6-Aug-07 18:59
professionalRavi Bhavnani6-Aug-07 18:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.